package com.lnyt.util.factroy;


import com.alibaba.fastjson.JSONArray;
import okhttp3.Cookie;
import okhttp3.CookieJar;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class OkHttpClientFactroy {
    public static OkHttpClient creator() {
        CookieJar cookieJar = new CookieJar() {
            private final Map<String, Cookie> cookieStore = new HashMap<>();

            @Override
            public void saveFromResponse(HttpUrl url, List<Cookie> cookies) {
                for (Cookie cookie : cookies) {
                    cookieStore.put(cookie.name(), cookie);
                }
            }

            @Override
            public List<Cookie> loadForRequest(HttpUrl url) {
                List<Cookie> cookies = new ArrayList<>(cookieStore.values());
                return cookies;
            }
        };

        OkHttpClient okHttpClient = new OkHttpClient.Builder().cookieJar(cookieJar).build();
        return okHttpClient;
    }
}
